//
//  AreaPickerView.m
//  areapicker
//
//  Created by Cloud Dai on 12-9-9.
//  Copyright (c) 2012年 clouddai.com. All rights reserved.
//

#import "AreaPickerViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "FMDatabase.h"

#define kDuration 0.3

@interface AreaPickerViewController ()
{

}

@end

@implementation AreaPickerViewController

@synthesize delegate=_delegate;
@synthesize locatePicker = _locatePicker;

- (void)dealloc
{
    [_locatePicker release];
    [_provinces release];
    [_cities release];
    [_areas release];
    [super dealloc];
}

-(IBAction)cancelPopUpView:(id)sender{
    [self cancelPicker];
}



-(void)viewWillAppear:(BOOL)animated{
    

    [super viewWillAppear:animated];
}

- (void)initWithStyle:(id<AreaPickerDelegate>)delegate
{
    self.delegate = delegate;
    
    self.locatePicker.dataSource = self;
    self.locatePicker.delegate = self;
    
    //加载数据
    self.areaDBPath = [[NSBundle mainBundle] pathForResource:@"area.nib" ofType:nil];
    self.dbManager = [FMDatabase databaseWithPath:self.areaDBPath];
    
    
    if (![_dbManager open]) {
        [_dbManager release];

    }
    
    int selectedProvinceIndex = 0;
    int selectedCityIndex = 0;
    FMResultSet *tmpSet ;
    NSMutableArray *tmpArr;
    NSString *sql ;
    
    //provinces
    tmpSet = [_dbManager executeQuery:@"select * from province"];
    tmpArr = [NSMutableArray array];
    
    while([tmpSet next]){
        NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
                              [tmpSet stringForColumn:@"pid"],@"pid", 
                              [tmpSet stringForColumn:@"name"],@"name"
                              , nil];

        [tmpArr addObject:dict];
    }
    self.provinces = [NSArray arrayWithArray:tmpArr];
    
    // citys
    NSString *pid = [[_provinces objectAtIndex:selectedProvinceIndex ] objectForKey:@"pid"];
    sql = [NSString stringWithFormat:@"select * from city where pid='%@'", pid];
    tmpSet = [_dbManager executeQuery:sql];
    tmpArr = [NSMutableArray array];
    while([ tmpSet next]){
        NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
                             [tmpSet stringForColumn:@"cityid"], @"cityid", 
                              [tmpSet stringForColumn:@"pid"], @"pid",
                               [tmpSet stringForColumn:@"name"],@"name",
                              [tmpSet stringForColumn:@"zipcode"],@"zipcode"
                              , nil];
        [tmpArr addObject:dict];
    }
    self.cities =  [NSArray arrayWithArray:tmpArr];
    
    // areas
    NSString *cityid = [[_cities objectAtIndex:selectedCityIndex] objectForKey:@"cityid"];
    sql = [NSString stringWithFormat:@"select * from area where cityid='%@'", cityid];
    tmpSet = [_dbManager executeQuery:sql];
    tmpArr = [NSMutableArray array];
    while([ tmpSet next]){
        NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
                              [tmpSet stringForColumn:@"areaid"], @"areaid",
                              [tmpSet stringForColumn:@"cityid"],@"cityid",
                              [tmpSet stringForColumn:@"name"],@"name",
                              [tmpSet stringForColumn:@"zipcode"],@"zipcode"
                              , nil];
        [tmpArr addObject:dict];
    }
    self.areas =  [NSArray arrayWithArray:tmpArr];
    
   
    [_dbManager close];
}



#pragma mark - PickerView lifecycle
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
        return 3;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    switch (component) {
        case 0:
            return [_provinces count];
            break;
        case 1:
            return [_cities count];
            break;
        case 2:
                return [_areas count];
                break;
        default:
            return 0;
            break;
    }
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
        switch (component) {
            case 0:
                return [[_provinces objectAtIndex:row] objectForKey:@"name"];
                break;
            case 1:
                return [[_cities objectAtIndex:row] objectForKey:@"name"];
                break;
            case 2:
                return [[_areas objectAtIndex:row] objectForKey:@"name"];

            default:
                return  @"";
                break;
        }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    
    NSString *cityid, *pid, *sql;
    
    if (![_dbManager open]) {
        [_dbManager release];

    }
    
    int selectedCityIndex = 0;
    FMResultSet *tmpSet ;
    NSMutableArray *tmpArr;
    
        switch (component) {
            case 0:
                
                pid = [[_provinces objectAtIndex:row ] objectForKey:@"pid"];
                sql = [NSString stringWithFormat:@"select * from city where pid='%@'", pid];
                tmpSet = [_dbManager executeQuery:sql];
                tmpArr = [NSMutableArray array];
                while([ tmpSet next]){
                    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
                                          [tmpSet stringForColumn:@"cityid"], @"cityid",
                                          [tmpSet stringForColumn:@"pid"], @"pid",
                                          [tmpSet stringForColumn:@"name"],@"name",
                                          [tmpSet stringForColumn:@"zipcode"],@"zipcode"
                                          , nil];
                    [tmpArr addObject:dict];
                }
                self.cities =  [NSArray arrayWithArray:tmpArr];
                // areas
                cityid = [[_cities objectAtIndex:selectedCityIndex] objectForKey:@"cityid"];
                sql = [NSString stringWithFormat:@"select * from area where cityid='%@'", cityid];
                tmpSet = [_dbManager executeQuery:sql];
                tmpArr = [NSMutableArray array];
                while([ tmpSet next]){
                    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
                                         [tmpSet stringForColumn:@"areaid"], @"areaid", 
                                          [tmpSet stringForColumn:@"cityid"],@"cityid", 
                                          [tmpSet stringForColumn:@"name"],@"name", 
                                          [tmpSet stringForColumn:@"zipcode"],@"zipcode"
                                          , nil];
                    [tmpArr addObject:dict];
                }
                self.areas =  [NSArray arrayWithArray:tmpArr];
                
                break;
            case 1:
                // areas
                cityid = [[_cities objectAtIndex:row] objectForKey:@"cityid"];
                sql = [NSString stringWithFormat:@"select * from area where cityid='%@'", cityid];
                tmpSet = [_dbManager executeQuery:sql];
                tmpArr = [NSMutableArray array];
                while([ tmpSet next]){
                    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
                                          [tmpSet stringForColumn:@"areaid"], @"areaid",
                                          [tmpSet stringForColumn:@"cityid"],@"cityid",
                                          [tmpSet stringForColumn:@"name"],@"name",
                                          [tmpSet stringForColumn:@"zipcode"],@"zipcode"
                                          , nil];
                    [tmpArr addObject:dict];
                }
                self.areas =  [NSArray arrayWithArray:tmpArr];
                
                break;
            case 2:
             
                break;
            default:
                break;
        }
    
    [_dbManager close];
    [self.locatePicker reloadAllComponents];
    NSString *provice = [[self.provinces objectAtIndex:[pickerView selectedRowInComponent:0]] objectForKey:@"name"];
    NSString *city = [[self.cities objectAtIndex:[pickerView selectedRowInComponent:1]] objectForKey:@"name"];
    NSString *area = [[self.areas objectAtIndex:[pickerView selectedRowInComponent:2]] objectForKey:@"name"];
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:provice,@"sheng",city,@"shi",area,@"qu",nil];


    [self.delegate pickerDidChaneStatus:self withlocationInformation:dic];
    
}


#pragma mark - animation

- (void)cancelPicker
{
    [self dismissModalViewControllerAnimated:YES];
}






@end
